using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Material.Entity;
using Material.Object;
using System.Configuration;
using Material.App;

namespace App.Material
{
    public partial class FrmKelompok : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        private TblKelompok  tblKelompok;
        private TblKelompokEntity tblKelompokEntity;
        private string stateInput = string.Empty;
        private int IdKelompok = 0;

        public FrmKelompok()
        {
            InitializeComponent();

            ClearTextbox();
            EnabledTextboxs(false, false);
            EnabledButtons(true, false, false, true);
            BindGridData();

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dataGridView1.AllowUserToResizeColumns = false;
        }

        /// <summary>
        /// Kondisi untuk Disable dan Enable Kontrol [Textbox dan Button]
        /// </summary>
        /// <param name="state">boolean True dan False</param>
        private void EnabledTextboxs(bool bltxtKelompok, bool bltxtKeterangan)
        {
            txtKelompok.Enabled = bltxtKeterangan;
            txtKeterangan.Enabled = bltxtKeterangan;

        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnCancel, bool blbtnSave, bool blbtnPrint)
        {
            btnNew.Enabled = blbtnNew;
            btnCancel.Enabled = blbtnCancel;
            btnSave.Enabled = blbtnSave;
            btnPrint.Enabled = blbtnPrint;

        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            txtKelompok.Text = string.Empty;
            txtKeterangan.Text = string.Empty;
        }

        

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtKelompok.Text.Trim().Length > 0)
            {
                tblKelompok = new TblKelompok();
                tblKelompokEntity = new TblKelompokEntity(connStr);
                int i = 0;

                tblKelompok.Kelompok = txtKelompok.Text;
                tblKelompok.Keterangan = txtKeterangan.Text;
                if (stateInput == "Add")
                {
                    tblKelompok.CreatedDate = DateTime.Now;
                    tblKelompok.CreatedBy = FrmLogin.UserID;
                    tblKelompok.ModifiedDate = DateTime.Now;
                    tblKelompok.ModifiedBy = FrmLogin.UserID;
                    i = tblKelompokEntity.Insert(tblKelompok);
                }
                else
                {
                    tblKelompok.Id = IdKelompok;
                    tblKelompok.ModifiedBy = FrmLogin.UserID;
                    tblKelompok.ModifiedDate = DateTime.Now;
                    i = tblKelompokEntity.Update(tblKelompok);
                }
                if (i != -1)
                {
                    ClearTextbox();
                    EnabledTextboxs(false, false);
                    EnabledButtons(true, false, false, true);
                    BindGridData();
                    MessageBox.Show("Simpan data berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                    MessageBox.Show("Simpan data tidak berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);


            }
            else
            {
                MessageBox.Show("Kolom Kelompok Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtKelompok.Focus();
            }




        }

        private void BindGridData()
        {
            tblKelompokEntity = new TblKelompokEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblKelompokEntity.GetDataAll();


        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(true, true);
            EnabledButtons(false, true, true, false);
            txtKelompok.Focus();
            stateInput = "Add";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(false, false);
            EnabledButtons(true, false, false, true);
            ClearTextbox();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 3:
                        ClearTextbox();
                        EnabledTextboxs(true, true);
                        EnabledButtons(false, true, true, false);
                        tblKelompokEntity = new TblKelompokEntity(connStr);
                        tblKelompok = new TblKelompok();
                        tblKelompok.Id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                        tblKelompok = tblKelompokEntity.GetDataByID(tblKelompok);
                        txtKeterangan.Text = tblKelompok.Keterangan;
                        txtKelompok.Text = tblKelompok.Kelompok;
                        IdKelompok = tblKelompok.Id;
                        stateInput = "Edit";
                        break;

                    case 4:
                        DialogResult dr = MessageBox.Show("Data akan dihapus?", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.Yes)
                        {
                            tblKelompokEntity = new TblKelompokEntity(connStr);
                            tblKelompok = new TblKelompok();
                            tblKelompok.Id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                            int i = tblKelompokEntity.Delete(tblKelompok);
                            BindGridData();
                        }
                        break;
                }

            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            FrmRptKelompok frm = new FrmRptKelompok();
            frm.ShowDialog();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            tblKelompok = new TblKelompok();
            tblKelompok.Kelompok = txtFind.Text;

            tblKelompokEntity = new TblKelompokEntity(connStr);

            dataGridView1.DataSource = tblKelompokEntity.GetFindData(tblKelompok).Tables[0];

        }
        

       
    }
}